<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Archive as Arc;
use app\admin\model\Article as Art;
use app\admin\model\Category as Cate;
use think\Request;
use think\Loader;
use think\Url;


class Article extends AdminBase
{
	public function index()
	{
		$archive = Arc::with('category,article')->select();

		// 获取用户的所有博客


		return (json($archive));
	}

	public function lists()
	{
		$this->assign('pageConfig',json_encode([]));
		return $this->fetch();
	}

	/**
	 * AJAX获取文章信息
	 * @return [type] [description]
	 */
	public function artData()
	{
		$pageIndex = $this->request->param('pageIndex');
		$pageSize = $this->request->param('pageSize');

		$where = [
			'attach_type'	=>	1,
			'status'		=>	0,
		];

		$count = Arc::has('article')->where($where)->count('id');
		$archive = Arc::has('article')->where($where)->limit(($pageIndex-1)*$pageSize,$pageSize)->order('create_time desc')->select();

		// 获取用户的所有博客

		$json = [];
		$json = [
			'rel'	=>	true,
			'msg'	=>	'获取成功',
			'list'	=>	$archive,
			'count'	=>	$count,
		];
		return json($json)
			->code(201)
			->header([
				'Cache-control' => 'no-cache,must-revalidate',
				'Access-Control-Allow-Origin' =>	'*',

				]);
	}

	public function test()
	{
		$blog          = new Arc;
		$blog->title   = 'thinkphp';
		$blog->content = ['data' => '实例内容'];
		$blog->together('content')->save();
	}






	public function create(Request $request)
	{
		$data = $request->param();

		$validate = Loader::validate('article');

		if(!$validate->check($data)){
			$this->error($validate->getError());
		}

		$archive = new Arc;

		$archive->allowField(true)->data($data);

		$article = new Art;

		$article->setData($data)->strict(false);


		$archive->article 		=	$article;

		if(!$archive->together('article')->save())
		{
			return $this->error($archive->getError(), Url::build('admin/article/index'));

		}

		Loader::model('BackstageLog')->record("新增文章,文档ID:[{$archive->id}]");
		return $this->success('文档新增成功', Url::build('admin/article/index'));
	}

/*	public function editSave($id,Request $request)
	{
		$data = $request->param();

		$arc = Arc::get($id,'article');

		dump($arc);
	}*/

	public function editSave($id)
	{
		
		$data = $this->request->param();

		$arc = Arc::get($id,'article');

		$validate = Loader::validate('article');

		if(!$validate->scene('edit')->check($data)){
			$this->error($validate->getError());
		}

		$arc->allowField(true)->data($data);


		//这里最后需要修改成自动属性?
		$arc->article->image 	= $this->request->param('image');
		$arc->article->content 	= $data['content'];

/*		//分开保存
		dump($arc->save());
		dump($arc->article->save());

		//共同保存
		//$arc->together('article')->save();*/
		
		if(!$arc->together('article')->save())
		{
			return $this->error(($arc->getError()?$arc->getError():'没有修改内容'),'index');
		}

		Loader::model('BackstageLog')->record("修改文章,文档ID:[{$id}]");

		return $this->success('文档修改成功', Url::build('index'));

	}

	public function test232()
	{
		$data = Loader::model('Category')->getCateTree();
		print_r($data);
	}

//界面化
	public function add()
	{
		$pageConfig = [];
		$pageConfig['up_url'] = Url::build('create');
		$this->assign('cateData',Loader::model('Category')->getCateTree());
		$this->assign('pageConfig',json_encode($pageConfig));
		return $this->fetch('add');
	}
	public function edit($id = 0)
	{

		//主题数据获取
		

		$data = Arc::with('category,article')->find($id);

		$pageConfig = [];
		$pageConfig['up_url'] = Url::build('editSave');

		$this->assign('cateData',Loader::model('Category')->getCateTree());
		$this->assign('pageConfig',json_encode($pageConfig));
		$this->assign('data',$data);

		return $this->fetch('form');
	}


	public function destroys()
	{
		header("Access-Control-Allow-Origin: *");
		header("Cache-control : no-cache,must-revalidate");


		$param = $this->request->param();

		$_c = $param['ids'];


		return json(['msg' => '模拟删除']);

		//return json(['msg'=>'共影响 '.count($_c).' 条数据','ids'=>$param['ids']]);

		//权限判断 是否有权限删除操作->是否有权限删除在下文章

		$return = Arc::destroy($param['ids']);


		Loader::model('BackstageLog')->record("删除文章,ID:[".json_encode($param['ids'])."]");

		return json(['msg'=>'共影响 '.$return." 条数据"]);
		
	}


	public function destroy($id)
	{
		if (Loader::model('Archive')->deleteArchive($id) === false) {
			return $this->error(Loader::model('Archive')->getError());
		}
		
		Loader::model('BackstageLog')->record("删除文章,ID:[{$id}]");

		return $this->success('文档删除成功', Url::build('admin/article/index'));
		
	}


	//一些测试方法
	public function validateTest()
	{
		$request = Request::instance();
		$data = $request->param();
		
		if(Loader::validate('Article')->check($data) == false)
		{
			return $this->error(loader::validate('Article')->getError());
		}

		return 'validate';

	}
}